The Goal 🎯
-
The Model: A simple social network.
- Users are represented as nodes in a graph.
- "Follows" are directed edges.
- The Task: Process commands to build the network and traverse it using DFS and BFS.
The Representation 💾
We will use an Adjacency List to store the graph.
The list at index `i` stores all the users that user `i` follows.
// Follows: 0->1, 0->2, 1->3
adj = [
0: [1, 2],
1: [3],
2: [],
3: [],
]
adj = [
0: [1, 2],
1: [3],
2: [],
3: [],
]
The Operations ⚙️
You will implement three commands:
add u vUser `u` follows `v`.
dfs uDepth-First Search from `u`.
bfs uBreadth-First Search from `u`.